package videoCourse;

public class BinarySearch {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};

        int begin = 0;

        int end = nums.length - 1;

        int mid = (begin + end) / 2;

        int index = -1;

        int target = 5;

        while (true) {
            if (nums[mid] == target) {
                index = mid;
                break;
            }

            if (target > nums[mid]) {
                begin = mid + 1;
            } else {
                end = mid - 1;
            }

            mid = (begin + end) / 2;
        }
        System.out.println(index);
    }
}
